package com.echo.code.demo;

import lombok.extern.log4j.Log4j2;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * Created by baiyp on 2019/05/16.
 */
@WebServlet(name = "post", urlPatterns = "/post")
@Log4j2
public class PostServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = req.getReader();
        String input;
        StringBuilder sb = new StringBuilder();
        while ((input = br.readLine()) != null) { //遍历body内容
            sb.append(input);
        }
        log.info("http body is {}", sb.toString());
        log.warn("dispatching...");
        RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
        rd.forward(req, resp);
        // resp.sendRedirect("index.jsp");
    }
}
